-----------------------------------------------------------------------------
-- 起名创角界面
--
---------------
-- Author: 筱迪
-- Date: 2020.12.14
-----------------------------------------------------------------------------
require "Common/define"
require "Logic/Config"
require "proto/login/message/ReqCreatePlayerMessage"

UICreateName = UIBase.New(PanelResNames.UICreateName)
local this = UICreateName

function UICreateName.Init()
    UIBase.Init(this)
end

function UICreateName:OnCreate()
    self.txtName = self:GetChild("NameInput")
    self.btnStart = self:GetChild("start")
    self.btnGotName = self:GetChild("randname")
    self.mState = self:GetController("state")

    self:InitData()
    self:BindEvent()
end

function UICreateName:InitData()
    self.titleNum = table.nums(G.dataTable["t_randomtitle"])
    self.nameNum = table.nums(G.dataTable["t_randomname"])
    self.txtName.promptText = getLanguage("Sys_CreateChara")

    --设置随机数种子
    math.randomseed(tostring(os.time()):reverse():sub(1, 7))
end

function UICreateName:BindEvent()
    self:RegisterOnClick(self.btnStart, self.OnClickStart)
    self:RegisterOnClick(self.btnGotName, self.OnClickRandom)
end

function UICreateName:OnClickRandom()
    local index = math.random(1, self.titleNum)
    local indexName = math.random(1, self.nameNum)
    local surname = ""
    local name = ""
    local cfgTitle = dataTable.getRow("t_randomtitle", index)
    if cfgTitle then
        surname = cfgTitle["f_Title"]
    end
    local cfgName = dataTable.getRow("t_randomname", indexName)
    if cfgName then
        name = cfgName["f_Name"]
    end
    self.randonNam = surname..name
    self.txtName.text = self.randonNam
end

function UICreateName:OnClickStart()
    if #self.txtName.text == 0 or self.txtName.text == "" then
        UISysTips.AddMsg("名字不能为空");
        return;
    end
    self.randonName = self.txtName.text;
    loginHandler.R_CreatePlayer(self.randonName)
end

function UICreateName:CheckNameOk()
    self.mState.selectedIndex = 1
    closeUI(PanelNames.UIWaiting)
    self:CheckNext()
end

function UICreateName:CheckNext()
    closeUI(PanelResNames.UICreateName)

    --soundMgr:EnabledBg(true)
end

return UICreateName;